/**
 * @description 配置用户
 * @author 李艳
 */

import React, {Component} from 'react'
import { message } from 'antd';
import connect from "react-redux/es/connect/connect";
import {show, hide} from '../../../configs/SharedReducer'
import TransferTable from "../../../commons/components/TransferTable";
import {organizationConfig} from "../../../configs/CommonComponentsConfig";
import {
  insertEmployeePositionDto, listAllAssignedEmployeesByPositionId, listAllCanAssignEmployees,
  removeEmployeePositionDto
} from "./PositionService";
import {Checkbox} from "antd";

class ConfigUser extends Component {
  handleLeftClick = async (rows) => {
    let ids = [];
    const {positionInfo} = this.props;
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await removeEmployeePositionDto({childId: positionInfo ? positionInfo.id : "", parentIds: ids}).then((data) => {
      const { status, message: msg } = data;
      if (status === 'SUCCESS') {
        message.success(msg);
      }
    }).catch(err => {
      this.props.hide()
    })

  }

  handleRightClick = async (rows) => {
    let ids = [];
    const {positionInfo} = this.props;
    for (let i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    await insertEmployeePositionDto({childId: positionInfo ? positionInfo.id : "", parentIds: ids}).then((data) => {
      const { status, message: msg } = data;
      if (status === 'SUCCESS') {
        message.success(msg);
      }
    }).catch(err => {
      this.props.hide()
    })
  };
//之前接口为真分页，现在改为假分页了
  leftService = async (params) => {
    const {positionInfo} = this.props;
    let result = [];
    // if (!params.pageInfo){
    //     params.pageInfo={page:1,rows:defaultPageSize}
    // }
    params.positionId = positionInfo.id;
    if (!params.selectedKey) {
      params.organizationId = positionInfo.organization ? positionInfo.organization.id : ""
    } else {
      params.organizationId = params.selectedKey;
      delete params.selectedKey
    }
    if (!params.includeSubNode) {
      params.includeSubNode = false
    }
    await listAllCanAssignEmployees(params).then((res) => {
      result = res;
    })
    return result;

  };
  rightService = async (params) => {
    let result = [];
    const {positionInfo} = this.props;
    await listAllAssignedEmployeesByPositionId({positionId: positionInfo.id ? positionInfo.id : ""}).then((res) => {
      result = res
    });
    return result;

  };

  JointQueryService = async (key, includeSubNode) => {
    const {positionInfo} = this.props;
    let params={};
    params.positionId = positionInfo.id;
    if (!key) {
      params.organizationId = positionInfo.organization ? positionInfo.organization.id : ""
    } else {
      params.organizationId = key;
    }
    params.includeSubNode = includeSubNode
    let result = [];
    await listAllCanAssignEmployees(params).then((res) => {
      result = res
    });
    return result;
  };

  render() {
    const leftColumns = [
      {
        title: '员工编号',
        dataIndex: 'code',
      },
      {
        title: '姓名',
        dataIndex: 'userName',
      },
      {
        title: '冻结',
        dataIndex: 'user.frozen',
        render: (text, record) => (
          <div style={{textAlign: "center"}}>
            <Checkbox checked={record.user ? record.user.frozen === true : false} disabled={true}/>
          </div>
        ),
      }
    ];
    const treeSelectConfig = {
      ...organizationConfig,
      lable: "组织机构",
      defaultValue: this.props.positionInfo.organization ? this.props.positionInfo.organization.id : ""
    };

    const checkBoxConfig = {lable: "包含子节点", service: listAllCanAssignEmployees};
    return (
      <TransferTable
        searchLeftKey={["userName","code"]}
        searchRightKey={["userName","code"]}
        treeSelectConfig={treeSelectConfig}
        checkBoxConfig={checkBoxConfig}
        handleLeftClick={this.handleLeftClick}
        handleRightClick={this.handleRightClick}
        rightService={this.rightService.bind(this)}
        leftService={this.leftService.bind(this)}
        leftColumns={leftColumns}
        rightColumns={leftColumns}
        JointQueryService={this.JointQueryService.bind(this)}
      />

    );
  }


}

const mapStateToProps = (state) => {
  return {};
}

const mapDispatchToProps = (dispatch) => {
  return {
    show: () => {
      dispatch(show())
    },
    hide: () => {
      dispatch(hide())
    }
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ConfigUser)


